<?php
/**
 * Created by PhpStorm.
 * User: kongqi
 * Date: 2019/12/2
 * Time: 21:25
 */

namespace App\Plugin\HostServer\Front;

use App\Plugin\HostServer\Services\DomainService;
use Illuminate\Http\Request;

class DomainController extends BaseController
{
    public $api;//注入api

    public function __construct()
    {
        parent::__construct();
        $this->api = new DomainService();
    }

    public function index(Request $request)
    {
        $domain = $request->input('domain');

        $title = config_cache_default('host_server.domain_seo_title', '注册域名');
        $key = config_cache_default('host_server.domain_seo_key', $title);
        $desc = config_cache_default('host_server.domain_seo_desc', $title);
        $this->setSeo($title, $key, $desc);
        //分割2前缀
        $result = [];
        if (!empty($domain)) {
            //$result = $this->api->search($domain);
        }
        $domains=\App\Plugin\HostServer\Models\PnHostSvDomain::domain();

        $result['domains']=$domains;
        $result['domain'] = $domain;

        return $this->display($result);
    }

    public function search(Request $request)
    {
        $domain = $request->input('domain');
        $ext = $request->input('ext');//指定了ext
        if($ext)
        {
            $ext=explode(",",$ext);
        }else{
            $ext=[];
        }


        $page = $request->input('page');
        if (empty($domain)) {
            return $this->returnErrorApi('缺少参数');
        }
        if (empty($page)) {
            return $this->returnErrorApi('缺少页数');
        }

        $result = $this->api->search($domain,$page,$ext);

        //dump($result);
        if ($result['code'] == 200) {
            if(empty($result['data']))
            {
                $html='';
            }else
            {
                $html = '';
                foreach ($result['data'] as $k => $v) {
                    $html .= '<tr>';
                    $html .= '<td>';
                    $html .= $v['domain'];
                    $html .= '</td>';
                    if ($v['code'] == -1) {
                        $html .= '<td colspan="3">暂不支持注册</td>';
                    } else {
                        $html .= '<td >' . ($v['has'] ? '' : ($v['price'] * 1)) . '</td>';
                        $html .= '<td >' . ($v['has'] ? '' : ($v['price'] +$v['renewal_price']*1)) . '</td>';
                        $html .= '<td >' . ($v['has'] ? '' : ($v['price'] +$v['renewal_price']*2)) . '</td>';

                    }
                    $html .= '<td>';
                    if ($v['has']) {
                        $html.='已注册';
                    }else{
                        $html.='<a href="'.plugin_route('hostserver.user.buy.domain',['domain'=>$v['domain']]).'" class="btn btn-custom-reverse">立即注冊</a>';
                    }
                    $html .= '</td>';
                    $html .= '</tr>';
                }
            }

            return $this->returnOkApi($result['msg'], $html);
        }
        return $this->returnErrorApi('当前查询人数过多，请稍后再试');
    }

}